'''
处理电流相关的值，直接对数据进行处理
'''

import re
from excutor.excutor import Excutor

class Current(Excutor):
    
    def __init__(self, data):
        super().__init__(data)
        ##取电流的key
        self.current = r'电流.*[)）]|电流$|^电流|保持电流|输入偏置电流类型|尾端电流控制'

        self.pattrenNum = re.compile(r'[.]*[0-9]+[.]?[0-9]*')
        self.patternUnit = re.compile(r'[kmµV]*A|[m]*V[ ]*DC|℃')
    
    #判断是否存在电流数据
    @staticmethod
    def Existence(data,
        current = r'电流.*[)）]|电流$|^电流|保持电流|输入偏置电流类型|尾端电流控制'):
        return data[data['new_key'].str.contains(current)]['new_key'].count()
    
    #修改数据表
    def processingData(self):
        indexCurrent = self.data[self.data['new_key'].str.contains(self.current)].index
        for index in indexCurrent:
            newValueRef = self._get_new_value(index)
            self.data.loc[index,'new_value_ref'] = newValueRef
        return self.data

    #分析并处理old_value,返回new_value_ref
    def _get_new_value(self,index):
        oneRowData = self.data.loc[index]
        old_value = oneRowData['old_value']
        numbers = self.pattrenNum.findall(old_value)
        numbers = [self._compelet_decimal(x) for x in numbers]
        units = self.patternUnit.findall(old_value)
        if not (numbers or units):
            return old_value
        elif numbers and units:
            return self._circumatance1(oneRowData,numbers,units)
        elif numbers:
            return self._circumatance2(oneRowData,numbers)
        else:
            return old_value
        
    #返回参考新值的第一种情况：数字和单位都存在
    def _circumatance1(self,oneRowData,numbers,units):
        if len(set(numbers)) == 1:
            return numbers[0] + units[0]
        elif len(numbers) == len(units):
            if 'V' in oneRowData['old_value'].upper() or '℃' in oneRowData['old_value']:
                value = ''
                for index,unit in enumerate(units):
                    if 'V' in unit.upper():
                        value += numbers[index] + unit.replace(' ','') + ','
                    elif '℃' in unit:
                        value += numbers[index] + unit
                    else:
                        value += numbers[index] + unit.replace(' ','')
                return value
            else:
                numbers = self._unify_the_units(numbers,units)
                if len(set(numbers)) == 1:
                    return numbers[0] + 'A'
                else:
                    numbers = self._sort_numbers(numbers)
                    return numbers[0] + ' to ' + numbers[-1] + 'A'
        else:
            numbers = self._sort_numbers(numbers)
            return numbers[0] + ' to ' + numbers[-1] + units[0]
                
    #返回参考新值的第二种情况：只有数字存在
    def _circumatance2(self,oneRowData,numbers):
        if len(set(numbers)) == 1:
            return numbers[0] + 'A'
        else:
            return numbers[0] + ' to ' + numbers[-1] + ' A '

    #统一电流单位为A,返回值是浮点数
    def _unify_the_units(self,numbers,units):
        numbers = self._str_to_float(numbers)
        for index,unit in enumerate(units):
            if unit == 'mA':
                numbers[index] = numbers[index] / 1000
            elif unit == 'µA':
                numbers[index] = numbers[index] / 1000000
        return [str(x) for x in numbers]
    
    #将数据排序,返回字符串
    def _sort_numbers(self,numbers):
        numbers = self._str_to_float(numbers)
        numbers.sort()
        return [str(x) for x in numbers]

    #将简写小数变为正常小数 例如.11
    def _compelet_decimal(self,num):
        if num[0] == '.':
            return '0' + num
        else:
            return num

    #将字符串数字改为浮点数
    def _str_to_float(self,numbers):
        return [float(x) for x in numbers]
    